import GameMap from "./GameMap";
import MonsterManager from "./MonsterManager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Monster extends cc.Component {

    @property
    speed = 100;

    private wayPoints = []
    
    private tiledMap:cc.TiledMap = null

    start () {
        // 获取GameMap脚本对象的wayPoints数组
        this.wayPoints = cc.find("Canvas/Map").getComponent(GameMap).wayPoints

        //设置怪物坐标为第一个路径点
        this.node.setPosition(this.wayPoints[0])
        let mts:cc.FiniteTimeAction[] = [];
        //按照路径点创建动作
        for (let index = 1; index < this.wayPoints.length; index++) {
            let time = cc.Vec2.distance(this.wayPoints[index - 1], this.wayPoints[index]) / this.speed
            let mt = cc.moveTo(time, this.wayPoints[index])
            mts.push(mt)
        }

        //添加回调动作
        let callFunc = cc.callFunc(()=>{
            this.destroySelf()
        })

        mts.push(callFunc)

        let seqAct = cc.sequence(mts)
        this.node.runAction(seqAct)
    }

    destroySelf(){
        MonsterManager.getInstance().collection(this.node)
        this.node.destroy();

        // 销毁其余子弹
        // 获取所有的子弹，销毁同一目标的子弹
    }

    //当碰撞进入
    onCollisionEnter(other:cc.Collider,self:cc.Collider){
        //销毁怪物
        //销毁子弹other
        //给Collider设置Tag
        if(1 == other.tag){
            other.node.destroy()
        }

        
    }

    update (dt) {
        
    }
}
